"""The entire tab for displaying a tray"""

from PyQt4 import QtGui
from tableDisplay import TableWidget
from search import ExpandingFilterWidget, FilterBar
from edit import ExpandingEditWidget
from functools import partial

class TabDisplay(QtGui.QTabWidget):
    """Displays all of the tabs as tabs"""
    def __init__(self, parent=None):
        QtGui.QTabWidget.__init__(self, parent)
        self.setTabsClosable(True)
        self.tabCloseRequested.connect(self.__removeTab)
        try:
            parent.setCentralWidget(self)
        except AttributeError:
            self.setSizePolicy(QtGui.QSizePolicy.Ignored,
                               QtGui.QSizePolicy.Ignored)
            self.updateGeometry()
        self.show()
        self.__activeWidgets=[]
        self.__knownWidgets={}
    def __removeTab(self, index):
        """Removes the tab and updates its active tab knowledge"""
        del self.__activeWidgets[index]
        self.removeTab(index) #The tab widget remove
    def activate(self, name):
        """Either adds in a widget, or switches to it, either way making it
        the active one"""
        activated=self.__knownWidgets[name]
        if activated in self.__activeWidgets:
            index=self.__activeWidgets.index(activated)
        else:
            index=self.addTab(activated, '&'+name)
            self.__activeWidgets.append(activated)
        self.setCurrentIndex(index)
        return index
    def addListener(self, name, listener):
        """Connects the listener to the element chosen signal of the widget"""
        self.__knownWidgets[name].sElementChosen.connect(listener)
    def addWidget(self, name, widget):
        """Stores a widget as a known widget allowing the user to select it"""
        self.__knownWidgets[name]=widget
        self.activate(name)
        #Thus, keeps the window from popping up separately
    def addMenu(self, menuBar, mainWindow):
        """To add the set of tabs into the main menu"""
        menu=menuBar.addMenu('&Tabs')
        for name in self.__knownWidgets:
            act=QtGui.QAction(name, mainWindow)
            act.triggered.connect(partial(self.activate, name))
            menu.addAction(act)

class TrayDisplay(QtGui.QWidget):
    """Displays the elements of a tray, the search widget and the playlist
    widget"""
    def __init__(self, profile, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.tableWidget=TableWidget(profile.elements, profile.atts)
        self.filterBar=FilterBar([], self)
        self.filterWidget=ExpandingFilterWidget(profile.atts,self.tableWidget,
                                                self.filterBar, self)
        self.editWidget=ExpandingEditWidget(profile.atts, self.tableWidget,
                                              self)
        self.layout=QtGui.QGridLayout(self)
        self.layout.addWidget(self.filterWidget, 0, 1)
        self.layout.addWidget(self.filterBar, 0, 0, 0, 1)
        self.layout.addWidget(self.editWidget, 0, 2, 0, 1)
        self.layout.addWidget(self.tableWidget, 1, 1)
